define(['app'], function (app) {
    app.register.controller('period', function ($scope, $http, $state, $stateParams, $sce,$rootScope,$timeout) {
        $scope.API = null;
        $scope.onPlayerReady = function(API) {
            $rootScope.API = API;
        };
        $scope.curriculum_id= $stateParams.curriculum_id;
        $rootScope.states=1;
        $scope.period_id = $stateParams.period_id;
        $scope.tipModal = {
			isShow: false,
			title: '温馨提示',
			data: {},
			confirm: function() {
				this.hide();
				var url = $state.href('app.trainCourse');
				window.location.href = url;
				window.location.reload(true);
			},
			cancel: function() {
				this.hide()
			},
			hide: function() {
				this.isShow = false;
			},
			show: function() {
				this.isShow = true;
			}
		}
        $scope.load = {
            _getPeriodInfoList: function () {
                $http({
                    url: app._baseUrl + 'Qy/Curriculum/lists&curriculum_id=' + $scope.curriculum_id,
                    method: 'GET',
                    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
                }).success(function (res) {
                    $scope.periodInfoList = res.data;
                      angular.forEach($scope.periodInfoList , function(v,k) {
                    	v.index=k
                    });
                })
            },
            _getTrainCourseinfo: function(){  //专业类别和专业
                $http({
                    url: app._baseUrl + 'Qy/Curriculum/info&curriculum_id=' +$scope.curriculum_id,
                    method: 'GET',
                    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
                }).success(function(res){
                    $scope.TrainCourseinfo = res.data;
                    $rootScope.secondary = false;
                });
            },
            init: function () {
            	$scope.TrainCourseinfo = [];
                $scope.periodInfoList =$scope.periodInfo= [];
                this._getPeriodInfoList && this._getPeriodInfoList();
                this._getTrainCourseinfo && this._getTrainCourseinfo();
            }
        };
       $scope.load.init && $scope.load.init();
        $scope.play = function (item,index1) {
            if(item){
                if(item.learned==-1){

				    return false;
			    }else{
                    $scope.period_id=item.period_id;
                }

            }
            $scope.chaper = function (index) {
                $scope.temp = index;
            };
        	$scope.sectemp = index1;
            $rootScope.playing = false;
                $http({
                    url: app._baseUrl + 'Qy/Curriculum/periodInfo&period_id='+$scope.period_id,
                    method: 'GET',
                    // dataType:'json',
                    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
                }).success(function (res) {
                    $scope.load.init && $scope.load.init();
                    $rootScope.playing=true;
                    $scope.periodInfo = res.data;
                            $scope.config = {
                                sources: [
                                    {src: $sce.trustAsResourceUrl($scope.periodInfo.video_url), type: "video/mp4"}],
                                tracks: [{
                                        src: "http://www.videogular.com/assets/subs/pale-blue-dot.vtt",
                                        kind: "subtitles",
                                        srclang: "en",
                                        label: "English",
                                        default: ""
                                    }
                                ],
                                theme: "bower_components/videogular-themes-default/videogular.css",
                                plugins: {
                                    poster: "images/radio.png"
                                }
                            };
                    $timeout(function () {
                        $rootScope.API.play();
                        // $scope.load.init && $scope.load.init();
                    }, 100);
                });
        };
       $scope.play();
    })
});
